package product

import (
	"fmt"
	"gitee.com/yunduansing/test/gormdao"
	"github.com/jinzhu/gorm"
)

var (
	productTable="product"
)

type ProductEntity struct {
	Id int `gorm:"primary_key"`
	Name string
	gorm.Model
	Deleted bool
	Enabled bool
}

func (p *ProductEntity) BeforeCreate(scope *gorm.Scope) error {
	scope.SetColumn("deleted",false)
	scope.SetColumn("enabled",true)
	return nil
}

func (p *ProductEntity) BeforeDelete(scope *gorm.Scope) error {
	scope.SetColumn("deleted",true)
	scope.SetColumn("enabled",false)
	return nil
}

func New(d *gormdao.GormDao) (err error) {
	p:=ProductEntity{
		Name:    "星巴克咖啡",
	}
	err=d.DB.Table(productTable).Create(&p).Error
	return err
}

func Delete(d *gormdao.GormDao) error {
	err:=d.DB.Table(productTable).Delete(&ProductEntity{Id: 6},"id=?",6).Error
	return err
}

func Search(d *gormdao.GormDao)  {
	var data []ProductEntity
	total:=0
	err:=d.DB.Table(productTable).Where("id in(?)",[]string{"1","2"}).Count(&total).Offset((1-1)*10).Limit(10).Find(&data).Error
	fmt.Println(total,data,err)
}
